/*
 * ChatServer.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package sample.comet

import scala.actors._
import Actor._


import net.liftweb._
import http._
import util._
import rest._

object ChatServer extends Actor {
  private var msgs: List[String] = Nil
  private var listeners: List[Actor] = Nil

  def act = loop {
    react {
      case s: String =>
        msgs = s :: msgs
        listeners.foreach(_ ! Update(msgs))

        case Listen(who) =>
          listeners = who :: listeners
          who ! Update(msgs)

        case Unlisten(who) =>
          listeners = listeners.filter(_ ne who)
    }
  }
  this.start
}

case class Update(msgs: List[String])
case class Listen(who: Actor)
case class Unlisten(who: Actor)
